{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### vector2d_v0.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from array import array\n",
    "\n",
    "\n",
    "class Vector2d:\n",
    "    typecode = 'd'\n",
    "\n",
    "    def __init__(self, x, y):\n",
    "        self.__x = float(x)\n",
    "        self.__y = float(y)\n",
    "\n",
    "    @property\n",
    "    def x(self):\n",
    "        return self.__x\n",
    "\n",
    "    @property\n",
    "    def y(self):\n",
    "        return self.__y\n",
    "\n",
    "    def __iter__(self):\n",
    "        return (i for i in (self.x, self.y))\n",
    "\n",
    "    def __repr__(self):\n",
    "       class_name = type(self).__name__\n",
    "       return '{}({!r}, {!r})'.format(class_name, *self)\n",
    "\n",
    "    def __str__(self):\n",
    "        return str(tuple(self))\n",
    "\n",
    "    def __bytes__(self):\n",
    "        return (bytes([ord(self.typecode)]) +\n",
    "                bytes(array(self.typecode, self)))\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return tuple(self) == tuple(other)\n",
    "\n",
    "    def __hash__(self):\n",
    "        return hash(self.x) ^ hash(self.y)\n",
    "\n",
    "    def __abs__(self):\n",
    "        return math.hypot(self.x, self.y)\n",
    "\n",
    "    def __bool__(self):\n",
    "        return bool(abs(self))\n",
    "\n",
    "    def angle(self):\n",
    "        return math.atan2(self.y, self.x)\n",
    "\n",
    "    def __format__(self, fmt_spec=''):\n",
    "        if fmt_spec.endswith('p'):\n",
    "            fmt_spec = fmt_spec[:-1]\n",
    "            coords = (abs(self), self.angle())\n",
    "            outer_fmt = '<{}, {}>'\n",
    "        else:\n",
    "            coords = self\n",
    "            outer_fmt = '({}, {})'\n",
    "        components = (format(c, fmt_spec) for c in coords)\n",
    "        return outer_fmt.format(*components)\n",
    "\n",
    "    @classmethod\n",
    "    def frombytes(cls, octets):\n",
    "        typecode = chr(octets[0])\n",
    "        memv = memoryview(octets[1:]).cast(typecode)\n",
    "        return cls(*memv)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3.0, 4.0)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Vector2d(3.0, 4.0)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = Vector2d(3, 4)\n",
    "print(v)\n",
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v == (3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(3.00, 4.00)'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(v, '.2f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<5.0, 0.93>'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(v, '.2p')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
